package GUI;

import GUITOOl.MyBirthdayChooseBox;
import GUITOOl.MyButton;
import GUITOOl.MyLineBorder;
import Service.UpdatePerInfo;
import Utils.GetWindowsSize;
import Utils.ImageIconUtils;
import Utils.ThreadManage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * The type Info frame.
 *
 * @author wyc,zry
 * @version 1.0
 */
public class InfoFrame extends JFrame {
    private JLabel id_lable;
    private JLabel proId_lable;
    private JLabel username_lable;
    private JLabel sex_lable;
    private JLabel birthday_lable;

    private JTextField id_text;
    private JTextField username_text;
    private JRadioButton boy_button;
    private JRadioButton girl_button;
    private ButtonGroup sex_button;

    private JButton proIdChoose;
    private JButton confirm_button;
    private JButton return_login;
    private MyBirthdayChooseBox birthdayChooseBox;

    /**
     * The My uid.
     */
    int my_uid;
    /**
     * The My name.
     */
    String my_name;
    /**
     * The Profile id.
     */
    int profileID;
    /**
     * The Sex.
     */
    String sex;
    /**
     * The Birthday.
     */
    String birthday;

    /**
     * The Box main.
     */
    Box boxMain,
    /**
     * The Box left.
     */
    boxLeft,
    /**
     * The Box right.
     */
    boxRight,
    /**
     * The Box sex.
     */
    boxSex,
    /**
     * The Box right birth.
     */
    boxRight_birth,
    /**
     * The Box up.
     */
    boxUp,
    /**
     * The Box down.
     */
    boxDown;

    /**
     * Instantiates a new Info frame.
     *
     * @param my_uid    the my uid
     * @param profileID the profile id
     * @param my_name   the my name
     * @param sex       the sex
     * @param birthday  the birthday
     */
    public InfoFrame(int my_uid,int profileID, String my_name, String sex, String birthday){
        this.my_uid = my_uid;
        this.profileID = profileID;
        this.my_name = my_name;
        this.sex = sex;
        this.birthday = birthday;
        init();
    }

    /**
     * Init.
     */
    public void init(){

        MyLineBorder textLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
        Color bgColor=new Color(255,255,255);

        setTitle("feather_char_changeInfo");
        setSize(480,500);
        setLocation(GetWindowsSize.getWindowsSize().width / 2 - this.getWidth() / 2, GetWindowsSize.getWindowsSize().height / 2 - this.getHeight() / 2);
        getContentPane().setBackground(Color.white);
        setResizable(false);
        setLayout(new FlowLayout());

        Image frameImage=ImageIconUtils.getRawPicture("picture.png").getImage();
        setIconImage(frameImage);
        setVisible(true);

        id_lable = new JLabel("用户id");
        id_lable.setFont(new Font("微软雅黑",0,16));
        proId_lable = new JLabel("头像");
        proId_lable.setFont(new Font("微软雅黑",0,16));
        username_lable = new JLabel("用户名");
        username_lable.setFont(new Font("微软雅黑",0,16));
        sex_lable = new JLabel("性别");
        sex_lable.setFont(new Font("微软雅黑",0,16));
        birthday_lable = new JLabel("出生日期");
        birthday_lable.setFont(new Font("微软雅黑",0,16));

        id_text = new JTextField(25);
        id_text.setEditable(false);
        id_text.setBorder(textLineBorder);
        id_text.setBounds(50,15,150,30);
        id_text.setText(my_uid + "");
        username_text = new JTextField(25);
        username_text.setBorder(textLineBorder);
        username_text.setBounds(50, 85, 150, 30);
        username_text.setText(my_name);
        proIdChoose = new MyButton("选择头像",1);
        proIdChoose.setSize(50,30);
        proIdChoose.addActionListener(new chooseProfile());
        boy_button = new JRadioButton("男");
        boy_button.setFont(new Font("微软雅黑",0,16));
        boy_button.setBackground(bgColor);
        girl_button = new JRadioButton("女");
        girl_button.setFont(new Font("微软雅黑",0,16));
        girl_button.setBackground(bgColor);
        if(sex.equals("男")){
            boy_button.setSelected(true);
        }else {
            girl_button.setSelected(true);
        }
        confirm_button = new MyButton("确认修改",1);
        confirm_button.setSize(50,30);
        confirm_button.addActionListener(new InfoFrame.confirmAction());
        return_login = new MyButton("返回",1);
        return_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                InfoFrame.this.dispose();
            }
        });
        confirm_button.setSize(100,30);
        sex_button = new ButtonGroup();
        sex_button.add(boy_button);
        sex_button.add(girl_button);

        boxLeft = Box.createVerticalBox();
        boxLeft.add(Box.createVerticalStrut(20));
        boxLeft.add(id_lable);
        boxLeft.add(Box.createVerticalStrut(20));
        boxLeft.add(proId_lable);
        boxLeft.add(Box.createVerticalStrut(25));
        boxLeft.add(username_lable);
        boxLeft.add(Box.createVerticalStrut(25));
        boxLeft.add(birthday_lable);
        boxLeft.add(Box.createVerticalStrut(35));
        boxLeft.add(sex_lable);

        String[] s = birthday.split("-");
        birthdayChooseBox = new MyBirthdayChooseBox(s[0], s[1], s[2]);
        boxRight_birth = birthdayChooseBox.getBirthdayChooseBox();
        boxSex=Box.createHorizontalBox();
        boxRight = Box.createVerticalBox();
        boxRight.add(Box.createVerticalStrut(50));
        boxRight.add(id_text);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(proIdChoose);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(username_text);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(boxRight_birth);
        boxSex.add(Box.createHorizontalStrut(3));
        boxSex.add(boy_button);
        boxSex.add(Box.createHorizontalStrut(8));
        boxSex.add(girl_button);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(boxSex);

        boxUp=Box.createHorizontalBox();
        boxUp.add(boxLeft);
        boxUp.add(Box.createHorizontalStrut(10));
        boxUp.add(boxRight);

        boxDown=Box.createHorizontalBox();
        boxDown.add(Box.createHorizontalStrut(3));
        boxDown.add(confirm_button);
        boxDown.add(Box.createHorizontalStrut(50));
        boxDown.add(return_login);

        boxMain=Box.createVerticalBox();
        boxMain.add(boxUp);
        boxRight.add(Box.createVerticalStrut(25));
        boxMain.add(boxDown);
        add(boxMain, BorderLayout.CENTER);

        JPanel panel_bottom=new JPanel();

        panel_bottom.setBackground(bgColor);
        ImageIcon image1=ImageIconUtils.getRawPicture("register_bottom.png");
        JLabel background1=new JLabel(image1);
        background1.setBounds(0,0,480,145);
        panel_bottom.add(background1);
        add(panel_bottom,BorderLayout.SOUTH);
        setVisible(true);
    }

    private class confirmAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = username_text.getText();
            if(username.trim().isEmpty()){
                new showMessageFrame("用户名不能为空");
            }else {
                String birthday = birthdayChooseBox.getComBox_year().getSelectedItem() + "-" + birthdayChooseBox.getComBox_month().getSelectedItem() + "-" + birthdayChooseBox.getComBox_day().getSelectedItem();
                String sex;
                if (boy_button.isSelected()){
                    sex = boy_button.getActionCommand();
                }else{
                    sex = girl_button.getActionCommand();
                }
                ThreadManage.getInstance().getClientThread(my_uid).getMainFrame().updateInfo(username, profileID, sex, birthday);
                UpdatePerInfo.updatePerInfo(my_uid, profileID, username, sex, birthday);
                new showMessageFrame("修改成功");
                InfoFrame.this.dispose();
            }
        }
    }

    /**
     * chooseProfile Listener
     * @author dzr
     * @version 1.0
     */
    private class chooseProfile implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog dialog = new JDialog(InfoFrame.this);
            dialog.setTitle("选择头像");
            dialog.setLayout(new GridLayout(3, 3));
            dialog.setSize(300, 300);
            dialog.setBackground(Color.white);
            JButton[] profiles = new JButton[9];
            for (int i = 0; i < profiles.length; i++) {
                profiles[i] = new JButton(i + "");
                profiles[i].setOpaque(false);
                profiles[i].setBackground(Color.WHITE);
                profiles[i].setSize(100, 100);
                profiles[i].setIcon(ImageIconUtils.getProfile(i, profiles[i].getWidth(), profiles[i].getHeight()));
                profiles[i].addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        profileID = Integer.parseInt(e.getActionCommand());
                        dialog.dispose();
                    }
                });
                dialog.add(profiles[i]);
            }
            dialog.setLocation(InfoFrame.this.getX() + InfoFrame.this.getWidth() / 2 - dialog.getWidth() / 2,
                    InfoFrame.this.getY() + InfoFrame.this.getHeight() / 2 - dialog.getHeight() / 2);
            dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            dialog.setResizable(false);
            dialog.setVisible(true);
        }
    }
}
